package com.mp.approvalreq.servlet.component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.common.Constants;
import com.mp.approvalreq.entity.dto.ComponentGroupDTO;
import com.mp.approvalreq.entity.dto.TemplateDTO;
import com.mp.approvalreq.entity.vo.ComponentGroupVO;
import com.mp.approvalreq.service.SelectApprovalServicePrx;
import com.mp.approvalreq.servlet.AbstractServlet;
import com.mp.approvalreq.util.ApprovalUtils;
import com.mp.approvalreq.util.ServiceUtils;
import com.mp.user.entity.User;
import com.plusmoney.message.SuccessMsg;
import com.plusmoney.util.ParameterUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liuqi
 * @date 2018/9/19
 */
@WebServlet("/getTemplateComponentGroupList")
public class GetTemplateComponentGroupList extends AbstractServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User user = getReqUser(req);

            Integer templateId = ParameterUtils.getIntegerParameter(req, "templateId", true);

            SelectApprovalServicePrx selectApprovalService = ServiceUtils.getSelectApprovalServicePrx();

            List<ComponentGroupVO> componentGroupVOs;

            TemplateDTO templateDTO = selectApprovalService.getTemplateDTO(user.getCompanyId(), templateId);

            if (StringUtils.isNotEmpty(templateDTO.getComponentGroupJson())) {
                String componentGroupIds = ApprovalUtils.getComponentGroupIds(templateDTO.getComponentGroupJson());
                List<ComponentGroupDTO> componentGroupDTOs = selectApprovalService.getComponentGroupInfoDTOs(user.getCompanyId(), componentGroupIds);

                componentGroupVOs = initComponentGroupVOs(componentGroupDTOs, templateDTO.getComponentGroupJson());
            } else {

                componentGroupVOs = new ArrayList<>();
            }

            printJson(resp, SuccessMsg.getDefaultMsg(componentGroupVOs));
        } catch (Throwable e) {
            onThrowable(req, resp, e);
        }
    }

    private List<ComponentGroupVO> initComponentGroupVOs(List<ComponentGroupDTO> componentGroupDTOs, String componengGroupJson) throws Exception {
        List<ComponentGroupVO> list = new ArrayList<>(componentGroupDTOs.size());

        for (ComponentGroupDTO componentGroupDTO : componentGroupDTOs) {
            ComponentGroupVO componentGroupVO = new ComponentGroupVO();
            componentGroupVO.setId(componentGroupDTO.getId());
            componentGroupVO.setName(componentGroupDTO.getName());
            componentGroupVO.setIsAddComponentGroup(componentGroupDTO.getIsAddComponentGroup());
            componentGroupVO.setComponents(componentGroupDTO.getComponents());
            componentGroupVO.setSize(componentGroupDTO.getSize());
            componentGroupVO.setIsCommonGroup(componentGroupDTO.getIsCommonGroup());

            list.add(componentGroupVO);
        }

        JSONArray jsonArray = JSONArray.parseArray(componengGroupJson);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            Integer id = object.getInteger(Constants.GROUP_ID);
            Integer num = object.getInteger(Constants.GROUP_NUM);

            for (ComponentGroupVO componentGroupVO : list) {
                if (id.equals(componentGroupVO.getId())) {
                    componentGroupVO.setNum(num);
                }
            }
        }

        return list;
    }

}
